package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.ShouldGatherPayCountAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ShouldGatherPayCount;
import com.isunland.managesystem.entity.ShouldGatherPayCount.GatherPayCount;
import com.isunland.managesystem.entity.ShouldGgatherPay;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 应付款统计
 *
 * @author wu
 */
public class ShouldCountListFragment extends BaseListFragment {

  public static final String EXTRA_TYPE =
      "com.isunland.managesystem.ui.ShouldCountListFragment.EXTRA_TYPE";
  public static final String VALUE_PAY =
      "com.isunland.managesystem.ui.ShouldCountListFragment.VALUE_PAY";
  public static final String VALUE_GATHER =
      "com.isunland.managesystem.ui.ShouldCountListFragment.VALUE_GATHER";
  public static final String EXTRA_PAY_VALUE =
      "com.isunland.managesystem.ui.ShouldCountListFragment.EXTRA_PAY_VALUE";
  public static final String EXTRA_GATHER_VALUE =
      "com.isunland.managesystem.ui.ShouldCountListFragment.EXTRA_GATHER_VALUE";
  private static final int REQUEST_QUERY = 0;
  private ShouldGatherPayCountAdapter mAdapter;
  private int mPage = 1;// 请求第几页的数据.
  private String mType;
  private ShouldGgatherPay mQuery = new ShouldGgatherPay();
  private ShouldGgatherPay mPayQuery;
  private ShouldGgatherPay mGatherQuery;

  @Override public void onCreate(Bundle savedInstanceState) {
    // 设置返回图标
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    mType = getArguments().getString(EXTRA_TYPE);
    mPayQuery = (ShouldGgatherPay) getArguments().getSerializable(EXTRA_PAY_VALUE);
    mGatherQuery = (ShouldGgatherPay) getArguments().getSerializable(EXTRA_GATHER_VALUE);
    if (VALUE_GATHER.equalsIgnoreCase(mType)) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.getCount);
    } else if (VALUE_PAY.equalsIgnoreCase(mType)) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.payCount);
    }
    mQuery.setContractDeliveryTime("OTHER_HELP");
    mQuery.setCustomer("PART_ID,PART_NAME");
    mQuery.setConutType("CONTRACT_KIND,CONTRACT_KIND_NAME");

    super.onCreate(savedInstanceState);
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mPullRefreshListView.setMode(Mode.BOTH);
  }

  @Override public String getUrl() {
    return ApiConst.URL_GATHER_PAY__CONTRACT_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    params.put("page", mPage + "");
    params.put("rows", "20");

    String time = mQuery.getContractDeliveryTime();
    String customer = mQuery.getCustomer();
    String countType = mQuery.getCountType();
    String gropbystr = "";
    if (!TextUtils.isEmpty(time)) {
      gropbystr = time;
    }
    if ((!TextUtils.isEmpty(time)) && (!TextUtils.isEmpty(customer))) {
      gropbystr = gropbystr + ",";
    }
    if (!TextUtils.isEmpty(customer)) {
      gropbystr = gropbystr + customer;
    }
    if ((!TextUtils.isEmpty(customer)) && (!TextUtils.isEmpty(countType))
        || (!TextUtils.isEmpty(countType)) && (!TextUtils.isEmpty(time))) {
      gropbystr = gropbystr + ",";
    }

    if (!TextUtils.isEmpty(countType)) {
      gropbystr = gropbystr + countType;
    }

    LogUtil.i("gropbystr===" + gropbystr);
    params.put("gropbystr", gropbystr);
    if (VALUE_GATHER.equalsIgnoreCase(mType)) {
      params.put("flag", "in");
      params.put("beginregDate", mGatherQuery.getStratTime());
      params.put("endregDate", mGatherQuery.getEndTime());
      params.put("partId", mGatherQuery.getPartId());
      params.put("partName", mGatherQuery.getPartName());
      params.put("jsbf", mGatherQuery.getPartPay());
      params.put("wjs", mGatherQuery.getNotPay());
      params.put("yjs", mGatherQuery.getHaspay());
    } else if (VALUE_PAY.equalsIgnoreCase(mType)) {
      params.put("flag", "out");
      params.put("beginregDate", mPayQuery.getStratTime());
      params.put("endregDate", mPayQuery.getEndTime());
      params.put("partId", mPayQuery.getPartId());
      params.put("partName", mPayQuery.getPartName());
      params.put("jsbf", mPayQuery.getPartPay());
      params.put("wjs", mPayQuery.getNotPay());
      params.put("yjs", mPayQuery.getHaspay());
    }

    return params;
  }

  @Override public void resolveResponse(String json) {
    ShouldGatherPayCount count = new Gson().fromJson(json, ShouldGatherPayCount.class);
    List<GatherPayCount> detail = count.getRows();
    if (detail != null && detail.size() == 0) {
      ToastUtil.show(R.string.that_is_all);
    }
    setAdapter(detail);
  }

  private void setAdapter(List<GatherPayCount> detail) {
    if (detail == null) {
      detail = new ArrayList<ShouldGatherPayCount.GatherPayCount>();
    }

    if (mAdapter == null) {
      mAdapter = new ShouldGatherPayCountAdapter(getActivity(), detail);
      setListAdapter(mAdapter);
      return;
    }
    if (mPage == 1) {
      mAdapter.clear();
    }
    mAdapter.addAll(detail);
    ((ShouldGatherPayCountAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    GatherPayCount count = mAdapter.getItem(position - 1);
    Intent intent = new Intent(getActivity(), ShouldCountDetailActivity.class);
    intent.putExtra(ShouldCountDetailFragment.EXTRA_VALUE, count);
    intent.putExtra(ShouldCountDetailFragment.EXTRA_TYPE, mType);
    startActivity(intent);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_query, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:

        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      case R.id.menu_item_query:
        Intent intent = new Intent(getActivity(), ShouldCountSearchActivity.class);
        intent.putExtra(ShouldCountSearchFragment.EXTRA_COUNT_TYPE, mType);
        intent.putExtra(ShouldCountSearchFragment.EXTRA_VALUE, mQuery);
        startActivityForResult(intent, REQUEST_QUERY);
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }

    if (requestCode == REQUEST_QUERY && data != null) {
      mQuery =
          (ShouldGgatherPay) data.getSerializableExtra(ShouldCountSearchFragment.EXTRA_COUNT_VALUE);
      //			volleyPost();
    }
    mPullRefreshListView.setMode(Mode.PULL_FROM_START);
    mPage = 1;
    volleyPost();
    mPullRefreshListView.setMode(Mode.BOTH);
    super.onActivityResult(requestCode, resultCode, data);
  }

  @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) {

    Mode mode = refreshView.getCurrentMode();
    if (mode.equals(Mode.PULL_FROM_START)) {
      mPage = 1;
    }
    if (mode.equals(Mode.PULL_FROM_END)) {
      mPage++;
    }
    volleyPost();
  }

  public static Fragment newInstance(String type, ShouldGgatherPay gatherQuery,
      ShouldGgatherPay payQuery) {
    Bundle bundle = new Bundle();
    bundle.putString(EXTRA_TYPE, type);
    bundle.putSerializable(EXTRA_GATHER_VALUE, gatherQuery);
    bundle.putSerializable(EXTRA_PAY_VALUE, payQuery);
    ShouldCountListFragment fragment = new ShouldCountListFragment();
    fragment.setArguments(bundle);
    return fragment;
  }
}
